package com.product.controller;

import com.product.enmu.HttpStatusEnum;
import com.product.handler.ParameterValidationException;
import com.product.result.ApiResult;
import com.product.service.IProductService;
import com.product.to.AddProductTo;
import com.product.to.UpdateProductTo;
import com.product.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "产品控制器")
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @Parameters({
            @Parameter(name = "size", description = "每页条数，默认10条"),
            @Parameter(name = "current", description = "分页页数，默认第一页"),
            @Parameter(name = "productName", description = "产品名字"),
            @Parameter(name = "timeOrderBy", description = "按照创建时间顺序排序,0->升序,1->降序"),
            @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    })
    @Operation(summary = "获取所有产品")
    @GetMapping("/all")
    private ApiResult<PageUtils> getAllProducts(@RequestParam(name = "size", defaultValue = "10") Integer size,
                                                @RequestParam(name = "current", defaultValue = "1") Integer current,
                                                @RequestParam(name = "productName", required = false) String productName,
                                                @RequestParam(name = "productId", required = false) Integer productId,
                                                @RequestParam(name = "timeOrderBy", defaultValue = "1") Integer timeOrderBy) {
        return ApiResult.ok(productService.getAllProduct(size, current, productName,productId, timeOrderBy));
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "添加产品")
    @PostMapping("/add")
    private ApiResult<Boolean> addProduct(@Valid @RequestBody AddProductTo product, BindingResult result) throws ParameterValidationException {
        if (result.hasErrors()) {
            StringBuilder errors = new StringBuilder();
            result.getAllErrors().forEach(error -> errors.append(error.getDefaultMessage()).append("\n"));
            throw new ParameterValidationException(errors.toString(), HttpStatusEnum.PARAM_ERROR.getCode());
        }
        productService.addProduct(product);
        return ApiResult.ok();
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "修改产品")
    @PostMapping("/update")
    private ApiResult<Boolean> updateProduct(@Valid @RequestBody UpdateProductTo product, BindingResult result) throws ParameterValidationException {
        if (result.hasErrors()) {
            StringBuilder errors = new StringBuilder();
            result.getAllErrors().forEach(error -> errors.append(error.getDefaultMessage()).append("\n"));
            throw new ParameterValidationException(errors.toString(), HttpStatusEnum.PARAM_ERROR.getCode());
        }
        productService.updateProduct(product);
        return ApiResult.ok();
    }

    @Parameters({
            @Parameter(name = "productId", description = "产品id"),
            @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    })
    @Operation(summary = "删除产品")
    @PostMapping("/delete")
    private ApiResult<Boolean> deleteProduct(@RequestParam(name = "productId") Integer productId) {
        productService.deleteProduct(productId);
        return ApiResult.ok();
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "批量删除产品")
    @PostMapping("/deletes")
    private ApiResult<Boolean> deleteProducts(@RequestBody List<Integer> productIds) {
        productService.deleteProducts(productIds);
        return ApiResult.ok();
    }
}
